<?php

include_once 'include.php';

$template = new template("Reminders");
$reminders = new reminders();
$menu = menu();
$use = $db->get_row("Select * from Users where id='".$_SESSION['id']."'");
$page = "addReminder";
if(isset ($_GET["page"]))$page = $_GET["page"];
$id =0;
if(isset ($_GET["id"]))$id = $_GET["id"];

$type =0;
if(isset ($_GET["type"]))$type = $_GET["type"];
switch ($page)
{
    case "list":
        $template->addMenuItem("New Donor", "donor.php?page=addDonor", "green");
        $template->addContent($donor->listDonor($message));
    break;
    case "item":
        $template->addMenuItem("New Communication", "communications.php?page=addCommunication&id=".$_GET["id"], "green");
        $template->addMenuItem("New Donation", "donations.php?page=addDonation&id=".$_GET["id"], "green");
        $template->addMenuItem("New Reminder", "donor.php?page=addDonor".$_GET["id"], "green");
        $template->addMenuItem("Delete Donor", "donor.php?page=delete&id=".$_GET["id"], "red");
        $template->addContent($donor->item($message));
        break;
    
     case "editDonor":
        $template->addContent($donor->editDonor($id));
        break;
    
    case "addReminder":
        $template->addContent($reminders->addReminder());
        break;
    
    case "added":
       $donor->added();
        break;
    
    case "delete":
       $donor->deleteDonor($id);
        break;
    default:
        break;
}
$template->showMenu($menu);
$template->showLogin("Welcome");
$template->showUser($use->name.' | <a href="logout.php" class="logout">Logout</a>');
$template->genPage();
class donor
{
    function donor()
    {
        
    }
    function listDonor($message)
    {
        global $db;
        $data = "";
        $data .= "<h1>Donor List</h1>";
        if($message != null)
        $data .= "<div class='valid_box'> ".$message."</div>";
        $results = $db->get_results("SELECT * FROM `donors` ");
        if($results != null)
        {
            $data .= '<table id="rounded-corner" summary="">
    <thead>
    	<tr>
            <th scope="col" class="rounded-company">Name</th>
            <th scope="col" class="rounded">Contact Person</th>
            <th scope="col" class="rounded">Contact Email</th>
            <th scope="col" class="rounded">Contact Number</th>
            <th scope="col" class="rounded">Status</th>
            <th scope="col" class="rounded-q4">Functions</th>
        </tr>
    </thead>
    <tbody>';
            foreach ($results as $result) {
                $data .= '
          <tr>
            <td><a href="donor.php?page=item&id='.$result->id.'">'.$result->name.'</a></td>
            <td>'.$result->contactPerson.'</td>
            <td>'.$result->contactEmail.'</td>
            <td>'.$result->contactNumber.'</td>
            <td>'.getDonorStatus($result->status).'</td>
            <td><a href="donor.php?page=delete&id='.$result->id.'" class="ask"><img src="images/trash.png" alt="" title="" border="0"></a></td>
        </tr> 
        ';
            }
            $data.= '    </tbody>
</table>';
        }else
        {
            $data .= "Sorry there are no Donors please add";
        }
        return $data;
    }
    function item($message)
    {
        global $db;
        $data;
        $id = $_GET["id"];
        $row = $db->get_row("SELECT * FROM  `donors` WHERE `id` = $id;");
        $data = "<h2>Donor Information</h2>";
         if($message != null)
        $data .= "<div class='valid_box'> ".$message."</div>";
        $data .= "<b><u>Name</u></b> : ".$row->name."<br />";
        $data .= "<b><u>Contact Person</u></b> : ".$row->contactPerson."<br />";
        $data .= "<b><u>Contact Email</u></b> : ".$row->contactEmail."<br />";
        $data .= "<b><u>Contact Number</u></b> : ".$row->contactNumber."<br />";
        $data .= "<b><u>Contact Details</u></b> : ".$row->contactDetails."<br />";
        $data .= "<b><u>Status</u></b> : ".getDonorStatus($row->status)."<br />";
        $data .= "<hr /><h2>Donor Interactions:</h2>";
        $results = $db->get_results("SELECT * FROM  `communications` WHERE `donorId` = $id;");
        if($results != null)
        {
            $data .= '<table id="rounded-corner" summary="">
    <thead>
    	<tr>
            <th scope="col" class="rounded-company">Date</th>
            <th scope="col" class="rounded">Details</th>
            <th scope="col" class="rounded-q4">Functions</th>
        </tr>
    </thead>
    <tbody>';
            foreach ($results as $result) {
                $data .= '
          <tr>
            <td>'.$result->timestamp.'</td>
            <td>'.$result->details.'</td>
            <td><a href="communications.php?page=delete&id='.$result->id.'" class="ask"><img src="images/trash.png" alt="" title="" border="0"></a></td>
        </tr> 
        ';
            }
            $data.= '    </tbody>
</table>';
        }else
        {
            $data .= "Sorry there are no Donor interactions please add";
        }
        $data .= "<hr /><h2>Donor Donations:</h2>";
        $results = $db->get_results("SELECT * FROM  `donations` WHERE `donorid` = $id;");
        if($results != null)
        {
            $data .= '<table id="rounded-corner" summary="">
    <thead>
    	<tr>
            <th scope="col" class="rounded-company">Date</th>
            <th scope="col" class="rounded">Amount</th>
            <th scope="col" class="rounded">Details</th>
            <th scope="col" class="rounded-q4">Functions</th>
        </tr>
    </thead>
    <tbody>';
            $amount = 0;
            foreach ($results as $result) {
                $amount += $result->amount;
                $data .= '
          <tr>
            <td>'.$result->timestamp.'</td>
            <td>'.$result->amount.'</td>
            <td>'.$result->details.'</td>
            <td><a href="donations.php?page=delete&id='.$result->id.'" class="ask"><img src="images/trash.png" alt="" title="" border="0"></a></td>
        </tr> 
        ';
            }
            $data.= '    </tbody>
                <tfoot>
    	<tr>
        	<td colspan="6" class="rounded-foot-left"><em>Total Amount of cash donated: R '.$amount.'</em></td>
        	<td class="rounded-foot-right">&nbsp;</td>

        </tr>
    </tfoot>
</table>';
        }else
        {
            $data .= "Sorry there are no Donations please add";
        }
        $data .= "<hr /><h2>Donor Reminders:</h2>";
        $results = $db->get_results("SELECT * FROM  `reminder` WHERE `type` = 1 & typeid = $id;");
        if($results != null)
        {
            $data .= '<table id="rounded-corner" summary="">
    <thead>
    	<tr>
            <th scope="col" class="rounded-company">Date</th>
            <th scope="col" class="rounded">Comment</th>
            <th scope="col" class="rounded">status</th>
            <th scope="col" class="rounded-q4">Functions</th>
        </tr>
    </thead>
    <tbody>';
            foreach ($results as $result) {
                $data .= '
          <tr>
            <td>'.$result->date.'</td>
            <td>'.$result->comment.'</td>
            <td>'.getReminderRepeatName($result->status).'</td>
            <td><a href="#" class="ask"><img src="images/trash.png" alt="" title="" border="0"></a></td>
        </tr> 
        ';
            }
            $data.= '    </tbody>
                <tfoot>
    	<tr>
        	<td colspan="6" class="rounded-foot-left"><em>Total Amount of cash donated: R '.$amount.'</em></td>
        	<td class="rounded-foot-right">&nbsp;</td>

        </tr>
    </tfoot>
</table>';
        }else
        {
            $data .= "Sorry there are no Donations please add";
        }
        
        return $data;
    }
    function editDonor($id)
    {
        $page = "";
        global $db;
        
        $donor = $db->get_row('Select * from donors where id ='.$id);
        $selected = array("","","","","");
        $selected[$donor->status -1] = "Selected='selected'";
        $page .= "<div class='form'>
         <form action='donor.php?page=update' method='post' class='niceform'>
         
                <fieldset>
                <input type ='hidden' value = '".$id."'name='id'/>
                    <dl>
                        <dt><label for='name'>Name:</label></dt>
                        <dd><input type='text' name='name' id='' size='54' value= '".$donor->name."' /></dd>
                    </dl>
                    <dl>
                        <dt><label for='person'>Contact Person:</label></dt>
                        <dd><input type='text' name='person' id='' size='54' value= '".$donor->contactPerson."'/></dd>
                    </dl>
                    
                    <dl>
                        <dt><label for='email'>Contact email:</label></dt>
                        <dd><input type='text' name='email' id='' size='54' value= '".$donor->contactEmail."' /></dd>
                    </dl>
                    <dl>
                        <dt><label for='number'>Contact Number:</label></dt>
                        <dd><input type='text' name='number' id='' size='54' value= '".$donor->contactNumber."'/></dd>
                    </dl>
                    
                    <dl>
                        <dt><label for='details'>Contact Details:</label></dt>
                        <dd><textarea name='details' id='' size='54' value= ''>".$donor->contactDetails."</textarea></dd>
                    </dl>        
                
                 <dl>
                        <dt><label for='status'>Select status:</label></dt>
                        <dd>
                            <select size='1' name='status' id=''>
                                <option value='1' ".$selected[0].">Prospective</option>
                                <option value='2' ".$selected[1].">Donated</option>
                                <option value='3' ".$selected[2].">Uninterested</option>
                                <option value='4' ".$selected[3].">Unknown</option>
                                <option value='5' ".$selected[4].">Confirmed</option>
                            </select>
                        </dd>
                    </dl>
                    ";
                    $page .= "<dl class='submit'>
                    <input type='submit' name='submit' id='submit' value='Update' />
                     </dl>
                    
                </fieldset>
                
         </form>
         </div>  ";        
        return $page;       
    }
    
    
    function updateDonor()
    {
        global $db;
       $id = $_POST['id']; 
       $donorname = $_POST['name']; 
       $person = $_POST['person']; 
       $email = $_POST['email']; 
       $number = $_POST['number'];
       $details = $_POST['details'];
    $status = $_POST['status'];
     // var_dump($_POST); die();
       
       $query = "UPDATE donors SET name = '".$donorname."', contactPerson = '".$person."', contactEmail ='".$email."', contactNumber ='".$number."', contactDetails = '".$details."', status = ".$status." WHERE id =".$id;
      // echo $query; die();
       $db->query($query);
       header("Location:donor.php?page=item&id=".$id);
       
    } 
    
    function added()
    {
        global $db;
       
       $donorname = $_POST['name']; 
       $person = $_POST['person']; 
       $email = $_POST['email']; 
       $number = $_POST['number'];
       $details = $_POST['details'];
    $status = $_POST['status'];
     // var_dump($_POST); die();
       
       $query = "Insert into donors (name, contactPerson, contactEmail, contactNumber, contactDetails, status,created) VALUES ('".$donorname."','".$person."','".$email."' ,'".$number."', '".$details."' , ".$status.", Now())";
      // echo $query; die();
       $db->query($query);
       header("Location:donor.php?page=list&message=Donor Added");
       
    }
    
 function addReminder()
    {
        $page = "<div class='form'>
         <form action='donor.php?page=added' method='post' class='niceform'>
         
                <fieldset>
                <input type ='hidden' value = ''name='id'/>
                    <dl>
                        <dt><label for='name'>Name:</label></dt>
                        <dd><input type='text' name='name' id='' size='54' value= '' /></dd>
                    </dl>
                    <dl>
                        <dt><label for='person'>Contact Person:</label></dt>
                        <dd><input type='text' name='person' id='' size='54' value= ''/></dd>
                    </dl>
                    
                    <dl>
                        <dt><label for='email'>Contact email:</label></dt>
                        <dd><input type='text' name='email' id='' size='54' value= '' /></dd>
                    </dl>
                    <dl>
                        <dt><label for='number'>Contact Number:</label></dt>
                        <dd><input type='text' name='number' id='' size='54' value= ''/></dd>
                    </dl>
                    
                    <dl>
                        <dt><label for='details'>Contact Details:</label></dt>
                        <dd><textarea name='details' id='' size='54' value= ''></textarea></dd>
                    </dl>        
                
                 <dl>
                        <dt><label for='status'>Select status:</label></dt>
                        <dd>
                            <select size='1' name='status' id=''>
                                <option value='1' >Prospective</option>
                                <option value='2' >Donated</option>
                                <option value='3' >Uninterested</option>
                                <option value='4' selected='selected'>Unknown</option>
                                <option value='5' >Confirmed</option>
                            </select>
                        </dd>
                    </dl>
                    ";
                    $page .= "<dl class='submit'>
                    <input type='submit' name='submit' id='submit' value='Add' />
                     </dl>
                    
                </fieldset>
                
         </form>
         </div>  ";     
        
        return $page;       
    }
    
      
    function deleteDonor($id)
    {
        Global $db;
        
        $db->query("DELETE FROM donors WHERE id = ".$id);
        header("Location:donor.php?page=list&message=Donor Deleted");
    }
}
?>
